package com.ovt.doorbell.payment.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ovt.doorbell.payment.api.response.DoorbellPaymentAPIResult;
import com.ovt.doorbell.payment.common.model.JsonDocument;
import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.Permission;
import com.ovt.doorbell.payment.dao.vo.PermissionGrant;
import com.ovt.doorbell.payment.service.PermissionService;
import com.ovt.doorbell.payment.service.exception.ServiceException;

@RequestMapping("/permission")
public class PermissionController
{
    @Autowired
    private PermissionService permissionService;
    
    private JsonDocument SUCCESS = DoorbellPaymentAPIResult.SUCCESS;
    
    @RequestMapping(method = RequestMethod.POST,value = "/grant")
    @ResponseBody
    public JsonDocument grantPermission(@RequestParam int userId, @RequestParam int permId) 
            throws ServiceException
    {
        permissionService.grantPermission(userId, permId);
        return SUCCESS;
    }

    @RequestMapping(method = RequestMethod.POST,value = "/revoke")
    @ResponseBody
    public JsonDocument revokePermission(@RequestParam int userId, @RequestParam int permId) 
            throws ServiceException
    {
        permissionService.revokePermission(userId, permId);
        return SUCCESS;
    }
    
    /**
     * Get permission list.
     * 
     * @param page
     * @param limit
     * @param sortBy
     * @param order
     * @return
     * @throws ServiceException
     */
    
    @RequestMapping(method = RequestMethod.GET,value = "/listAll")
    @ResponseBody
    public JsonDocument getPermissionList(@RequestParam(required = false,
        defaultValue = "0") String pageno, @RequestParam(required = false,
        defaultValue = "20") String limit, @RequestParam(required = false,
        defaultValue = "id") String sortBy, @RequestParam(required = false,
        defaultValue = "asc") String order) throws ServiceException
    {
        
        PageInfo page =
                new PageInfo(DataConvertUtils.toInt(pageno),
                        DataConvertUtils.toInt(limit), sortBy, order);

        List<Permission> permissionList = permissionService.getPermissionList(page);

        return new DoorbellPaymentAPIResult(permissionList);
    }
    
    @RequestMapping(method = RequestMethod.GET,value = "/{id}/permitedUsers")
    @ResponseBody
    public JsonDocument getPermissionByUserId(@RequestParam(required = false,
        defaultValue = "0") String pageno, @RequestParam(required = false,
        defaultValue = "20") String limit, @RequestParam(required = false,
        defaultValue = "id") String sortBy, @RequestParam(required = false,
        defaultValue = "asc") String order, @PathVariable int id) throws ServiceException
    {
        PageInfo page =
                new PageInfo(DataConvertUtils.toInt(pageno),
                        DataConvertUtils.toInt(limit), sortBy, order);

        List<PermissionGrant> permissionList = permissionService.getPermissionGrantByUserId(page, id);

        return new DoorbellPaymentAPIResult(permissionList);
    }
    
}
